﻿using Newtonsoft.Json.Linq;
using NPortableEngine;
using Services4KouDai.Models;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Services4KouDai.Parsers
{
    public class GoodsDetailParser : IParser
    {
        private Platform _platform;

        public GoodsDetailParser(Platform platform)
        {
            this._platform = platform;
        }

        public override void ParseJson(string json, BackData backData)
        {
            Debug.WriteLine(json);
            GoodsDetail goods = (GoodsDetail)backData.ResponseData;

            if (_platform == Platform.WP8)
            {
                JArray jarray = JArray.Parse(json);
                JToken jt = jarray[0];
                OperationJToken(goods, jt);
            }
            else if (_platform == Platform.WIN8)
            {
                JObject jobj = JObject.Parse(json);
                JToken jt = jobj.SelectToken("result");
                OperationJToken(goods, jt);
            }
        }

        private void OperationJToken(GoodsDetail goods, JToken jt)
        {
            goods.Name = jt.Value<string>("name");
            goods.ItemUrl = jt.Value<string>("itemUrl");
            goods.Favorite = jt.Value<int>("favorite");
            goods.ImageUrl = jt.Value<string>("imageUrlForIphone");
            goods.ImageRatio = jt.Value<double>("imgRatio");
            goods.Price = jt.Value<string>("price");
            goods.MsrPrice = jt.Value<string>("msrprice");
            goods.SoldOut = jt.Value<string>("soldout");
            goods.CombineAppId = jt.Value<string>("combine_app_id");
            goods.OpenYxb = jt.Value<string>("openYxb");
            goods.CurrentTime = jt.Value<long>("currentTime");
            goods.BuyOpenType = jt.Value<string>("buy_open_type");
            goods.IsPreference = jt.Value<bool>("isPreference");
        }
    }
}
